package com.carpool;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import com.carpool.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MyStep1Activity extends Activity {
	final int PASS_CONTACT = 1;

	private LocationManager locManager;
	private LocationListener locListener;
	private Button btnHere, btnNext;
	private TextView txtCustomAddress, txtStatus;

	ArrayList<String> addresses = new ArrayList<String>();
	String myAddress;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.step1);
		
		txtStatus = (TextView)findViewById(R.id.txtStatus);

		locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		String provider = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ? "Provider enabled and searching..." : "Provider disabled";
		txtStatus.setText("GPS Status: " + provider);
		locListener = new LocationListener() {
			public void onLocationChanged(Location newLocation) {
				Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
				try {
					List<Address> address = geoCoder.getFromLocation(newLocation.getLatitude(), newLocation.getLongitude(), 1);
					if (address.size() > 0) 
					{
						myAddress = address.get(0).getAddressLine(0) + "\n" + address.get(0).getAddressLine(1);
						Toast.makeText(getBaseContext(), "Found your location.", Toast.LENGTH_SHORT).show();
						txtStatus.setText("GPS Status: Found\n" + address.get(0).getAddressLine(0) + ",\n" + address.get(0).getAddressLine(1));
						locManager.removeUpdates(locListener);
					}
					else {
						Toast.makeText(getBaseContext(), "Current location cannot be found.", Toast.LENGTH_SHORT).show();
					}
				} catch (IOException e) {
				} catch (NullPointerException e) {
				};
			}

			public void onProviderDisabled(String arg0) {
				txtStatus.setText("GPS Status: Provider disabled.");
			}

			public void onProviderEnabled(String arg0) {
				txtStatus.setText("GPS Status: Provider enabled. Acquiring current location.");
			}

			public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
			}
		};
		//locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
		locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
		
		btnHere = (Button)findViewById(R.id.btnHere);
		btnHere.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				if (myAddress == null) {
					Toast.makeText(getBaseContext(), "Current location cannot be found.", Toast.LENGTH_LONG).show();
				}
				else {
					addresses.add(myAddress);
					Intent intent = new Intent(getApplicationContext(), MyStep2Activity.class);
					Bundle bundle = new Bundle();
					bundle.putStringArrayList("addresses", addresses);
					intent.putExtras(bundle);
					startActivity(intent);
					finish();
				}
			}
		});

		txtCustomAddress = (TextView)findViewById(R.id.txtCustomAddress);
		btnNext = (Button)findViewById(R.id.btnNext);
		btnNext.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
				try {
					List<Address> address = geoCoder.getFromLocationName(txtCustomAddress.getText().toString(), 1);
					if (address.size() > 0) 
					{
						addresses.add(address.get(0).getAddressLine(0) + ", " + address.get(0).getAddressLine(1));
						Intent intent = new Intent(getApplicationContext(), MyStep2Activity.class);
						Bundle bundle = new Bundle();
						bundle.putStringArrayList("addresses", addresses);
						intent.putExtras(bundle);
						startActivity(intent);
						finish();
					}
					else {
						Toast.makeText(getBaseContext(), "No address could be found.", Toast.LENGTH_LONG).show();
					}
				} catch (IOException e) {          
				} catch (NullPointerException e) {
				};
			}
		});
		
	}
	
}